package com.daidai.activitys

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.os.Bundle
import android.view.GestureDetector
import android.view.MotionEvent
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import com.daidai.tools.G

class RollView(ctx: Context) : View(ctx) {

    var screenWidth: Int
    var paint: Paint
    var paint2: Paint = Paint(Paint.ANTI_ALIAS_FLAG)

    val lstItems = ArrayList<Item>()

    var offsetY = 0f

    // 是否循环
    var bRecyle = false

    inner class Item(var str: String) {

        var x = 0f
        var y = 0f
        var width = 512f
        var height = 100f
        var color = Color.WHITE

        fun draw(canvas: Canvas) {
            paint.color = Color.BLACK
            paint.textSize = 40f
            val center = y + height / 2f
            val fm = paint.fontMetrics
            val textHeight = fm.bottom - fm.top
            val finalY = center + textHeight / 2f - fm.bottom

            val textWidth = paint.measureText(str)
            canvas.drawText(str,(width - textWidth)/2f , finalY, paint)
        }
    }

    var listener = object : GestureDetector.SimpleOnGestureListener() {
        override fun onScroll(e1: MotionEvent?, e2: MotionEvent?, distanceX: Float, distanceY: Float): Boolean {
            G.elog("onScroll called! distanceX:$distanceX,distanceY:$distanceY")

            // distanceY 是上次 y - 这次的 y
            offsetY += distanceY

            // 循环滚动调整顺序
            // 是否需要循环
            if (bRecyle) {
                // 是否
            } else {
                // 是否滑动到底了?

            }

            invalidate()
            return true
        }

        override fun onFling(e1: MotionEvent?, e2: MotionEvent?, velocityX: Float, velocityY: Float): Boolean {
            G.elog("onFling called!  velocityX:$velocityX,  velocityY:$velocityY")
            return true
        }
    }
    var detector: GestureDetector

    init {
        screenWidth = ctx.resources.displayMetrics.widthPixels
        paint = Paint(Paint.ANTI_ALIAS_FLAG)
        paint.color = Color.RED
        paint2.color = Color.parseColor("#33ff0000")

        //
        val colos = intArrayOf(Color.RED, Color.GREEN, Color.YELLOW, Color.CYAN, Color.MAGENTA, Color.BLUE, Color.DKGRAY)
        var counter = 0
        colos.forEach {
            val item = Item("counter:$counter")
            item.color = it
            lstItems.add(item)
            counter++
        }

        detector = GestureDetector(ctx, listener)
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        setMeasuredDimension(512, 500)
    }

    override fun onDraw(pcanvas: Canvas?) {
        super.onDraw(pcanvas)

        val canvas = pcanvas!!

        // 绘制一个背景
        paint.color = Color.parseColor("#0a000000")
        canvas.drawRect(0f, 0f, width.toFloat(), height.toFloat(), paint)

        // 绘制5个 item
        var tmpY = offsetY
        lstItems.forEach { item ->
            item.y = tmpY
            item.draw(canvas)
            tmpY += item.height
        }


        // 绘制一个幕布
        canvas.drawRect(0f, (height - 100f) / 2f, 512f, (height - 100f) / 2f + 100f, paint2)

    }

    override fun onTouchEvent(event: MotionEvent?): Boolean {
        detector.onTouchEvent(event)
        return true
    }
}


class RollViewActivity : AppCompatActivity() {
    lateinit var rollView: RollView
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        rollView = RollView(this)
        rollView.x = 100f
        rollView.y = 100f
        setContentView(rollView)
    }
}